<template>
  <div v-loading="loading">
    <p style="color: red;font-size: 12px;">通过 activeMethod 方法返回 true 或 false 来决定该单元格是否允许编辑；通过监听 edit-disabled 事件用于提示信息</p>
    <p style="color: red;font-size: 12px;">未启用的行禁止编辑；已经设置过性别，不允许再次修改；年龄列禁止编辑</p>

    <div class="click-table10-oper">
      <el-button type="success" size="mini" @click="insertEvent">新增</el-button>
      <el-button type="danger" size="mini" @click="pendingRemoveEvent">标记/取消删除</el-button>
      <el-button type="danger" size="mini" @click="deleteSelectedEvent">删除选中</el-button>
      <el-button type="warning" size="mini" @click="submitEvent">保存</el-button>
      <el-button type="success" size="mini" @click="exportCsvEvent">导出</el-button>
    </div>

    <elx-editable
      ref="elxEditable"
      class="click-table10"
      border
      height="466"
      size="small"
      :data.sync="list"
      :row-class-name="tableRowClassName"
      :edit-config="{trigger: 'click', mode: 'cell', activeMethod}"
      @edit-active="editActiveEvent"
      @blur-active="blurActiveEvent"
      @edit-disabled="editDisabledEvent"
      @clear-active="clearActiveEvent"
      style="width: 100%">
      <elx-editable-column type="selection" width="55"></elx-editable-column>
      <elx-editable-column prop="id" label="ID" width="80"></elx-editable-column>
      <elx-editable-column prop="name" label="名字" show-overflow-tooltip :edit-render="{type: 'default'}">
        <template v-slot:edit="scope">
          <el-input v-model="scope.row.name" size="mini" @input="$refs.elxEditable.updateStatus(scope)"></el-input>
        </template>
        <template v-slot="scope">{{ scope.row.name }}</template>
      </elx-editable-column>
      <elx-editable-column prop="sex" label="性别" :edit-render="{type: 'default'}">
        <template v-slot:edit="scope">
          <el-select v-model="scope.row.sex" size="mini" clearable @change="$refs.elxEditable.updateStatus(scope)">
            <el-option v-for="item in sexList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </template>
        <template v-slot="scope">{{ getSelectLabel(scope.row.sex, 'value', 'label', sexList) }}</template>
      </elx-editable-column>
      <elx-editable-column prop="age" label="年龄" :edit-render="{type: 'default'}">
        <template v-slot:edit="scope">
          <el-input-number v-model="scope.row.age" size="mini" :min="1" :max="200" @input="$refs.elxEditable.updateStatus(scope)"></el-input-number>
        </template>
        <template v-slot="scope">{{ scope.row.age }}</template>
      </elx-editable-column>
      <elx-editable-column prop="region" label="地区" width="200" :edit-render="{type: 'default'}">
        <template v-slot:edit="scope">
          <el-cascader v-model="scope.row.region" size="mini" clearable :options="regionList" @change="$refs.elxEditable.updateStatus(scope)"></el-cascader>
        </template>
        <template v-slot="scope">{{ getCascaderLabel(scope.row.region, regionList) }}</template>
      </elx-editable-column>
      <elx-editable-column prop="date" label="日期" :edit-render="{type: 'default'}">
        <template v-slot:edit="scope">
          <el-date-picker v-model="scope.row.date" type="datetime" format="yyyy/MM/dd" size="mini" @change="$refs.elxEditable.updateStatus(scope)"></el-date-picker>
        </template>
        <template v-slot="scope">{{ getDatePicker(scope.row.date) }}</template>
      </elx-editable-column>
      <elx-editable-column prop="flag" label="行是否允许编辑" :edit-render="{type: 'visible'}">
        <template v-slot:edit="scope">
          <el-switch v-model="scope.row.flag" size="mini" @change="$refs.elxEditable.updateStatus(scope)"></el-switch>
        </template>
      </elx-editable-column>
      <elx-editable-column prop="updateTime" label="更新时间" width="160" :formatter="formatterDate"></elx-editable-column>
      <elx-editable-column prop="createTime" label="创建时间" width="160" :formatter="formatterDate"></elx-editable-column>
      <elx-editable-column label="操作" width="160">
        <template v-slot="scope">
          <el-button size="mini" type="danger" @click="removeEvent(scope.row)">删除</el-button>
          <el-button size="mini" type="info" @click="revertEvent(scope.row)">还原</el-button>
        </template>
      </elx-editable-column>
    </elx-editable>

    <el-pagination
      class="click-table10-pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageVO.currentPage"
      :page-sizes="[5, 10, 15, 20, 50, 100, 150, 200]"
      :page-size="pageVO.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageVO.totalResult">
    </el-pagination>
  </div>
</template>

<script>
import XEUtils from 'xe-utils'
import XEAjax from 'xe-ajax'
import { MessageBox, Message } from 'element-ui'

export default {
  data () {
    return {
      loading: false,
      sexList: [],
      regionList: [],
      list: [],
      pageVO: {
        currentPage: 1,
        pageSize: 10,
        totalResult: 0
      },
      pendingRemoveList: []
    }
  },
  created () {
    this.findSexList()
    this.findRegionList()
    this.findList()
  },
  methods: {
    findList () {
      this.loading = true
      this.pendingRemoveList = []
      XEAjax.doGet(`/api/user/page/list/${this.pageVO.pageSize}/${this.pageVO.currentPage}`).then(response => {
        let { page, result } = response.data
        this.list = result.map(item => {
          item.hasSex = item.sex != null
          return item
        })
        this.pageVO.totalResult = page.totalResult
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    findSexList () {
      XEAjax.doGet('/api/conf/sex/list').then(({ data }) => {
        this.sexList = data
      })
    },
    findRegionList () {
      XEAjax.doGet('/api/conf/region/list').then(({ data }) => {
        this.regionList = data
      })
    },
    searchEvent () {
      this.pageVO.currentPage = 1
      this.findList()
    },
    activeMethod ({ row, column }) {
      return this.checkEditStatus(row, column).status
    },
    handleSizeChange (pageSize) {
      this.pageVO.pageSize = pageSize
      this.findList()
    },
    handleCurrentChange (currentPage) {
      this.pageVO.currentPage = currentPage
      this.findList()
    },
    getSelectLabel (value, valueProp, labelProp, list) {
      let item = XEUtils.find(list, item => item[valueProp] === value)
      return item ? item[labelProp] : null
    },
    getCascaderLabel (value, list) {
      let values = value || []
      let labels = []
      let matchCascaderData = function (index, list) {
        let val = values[index]
        if (list && values.length > index) {
          list.forEach(item => {
            if (item.value === val) {
              labels.push(item.label)
              matchCascaderData(++index, item.children)
            }
          })
        }
      }
      matchCascaderData(0, list)
      return labels.join(' / ')
    },
    getDatePicker (value) {
      return XEUtils.toDateString(value, 'yyyy/MM/dd')
    },
    formatterDate (row, column, cellValue, index) {
      return XEUtils.toDateString(cellValue, 'yyyy-MM-dd HH:mm:ss')
    },
    tableRowClassName ({ row, rowIndex }) {
      if (this.pendingRemoveList.some(item => item === row)) {
        return 'delete-row'
      }
      return ''
    },
    editActiveEvent (row, column) {
      console.log('触发 edit-active 事件')
    },
    blurActiveEvent (row, column) {
      console.log('触发 blur-active 事件')
    },
    editDisabledEvent (row, column) {
      let rest = this.checkEditStatus(row, column)
      if (!rest.status) {
        Message({
          type: 'error',
          message: rest.message
        })
        console.log('触发 edit-disabled 事件')
      }
    },
    clearActiveEvent (row, column) {
      console.log('触发 clear-active 事件')
    },
    checkEditStatus (row, column) {
      let message = ''
      let status = true
      if (!row.flag) {
        status = false
        message = '请先设置允许编辑，然后再修改'
      } else if (['age'].includes(column.property)) {
        status = false
        message = '年龄不允许修改'
      } else if (['sex'].includes(column.property) && row.hasSex) {
        status = false
        message = '已经设置过性别，不允许再次修改'
      }
      return { status, message }
    },
    insertEvent () {
      this.$refs.elxEditable.insert({
        name: `New ${Date.now()}`,
        age: 26,
        flag: true
      }).then(({ row }) => {
        this.$refs.elxEditable.setActiveRow(row)
      })
    },
    removeEvent (row) {
      if (row.id) {
        MessageBox.confirm('确定永久删除该数据?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          XEAjax.doDelete(`/api/user/delete/${row.id}`).then(({ data }) => {
            this.findList()
          }).catch(e => {
            this.loading = false
          })
        }).catch(e => e)
      } else {
        this.$refs.elxEditable.remove(row)
      }
    },
    revertEvent (row) {
      if (this.$refs.elxEditable.hasRowChange(row)) {
        this.$refs.elxEditable.clearActive()
        this.$refs.elxEditable.revert(row)
        Message({
          type: 'success',
          message: '数据已还原！'
        })
      } else {
        Message({
          type: 'info',
          message: '数据未改动！'
        })
      }
    },
    pendingRemoveEvent () {
      let selection = this.$refs.elxEditable.getSelecteds()
      if (selection.length) {
        let plus = []
        let minus = []
        selection.forEach(data => {
          if (this.pendingRemoveList.some(item => data === item)) {
            minus.push(data)
          } else {
            plus.push(data)
          }
        })
        if (minus.length) {
          this.pendingRemoveList = this.pendingRemoveList.filter(item => minus.some(data => data !== item)).concat(plus)
        } else if (plus) {
          this.pendingRemoveList = this.pendingRemoveList.concat(plus)
        }
        this.$refs.elxEditable.clearSelection()
      } else {
        Message({
          type: 'info',
          message: '请至少选择一条数据！'
        })
      }
    },
    deleteSelectedEvent () {
      let removeRecords = this.$refs.elxEditable.getSelecteds()
      if (removeRecords.length) {
        MessageBox.confirm('确定删除所选数据?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          XEAjax.doPost('/api/user/save', { removeRecords }).then(({ data }) => {
            Message({
              type: 'success',
              message: '删除成功!'
            })
            this.findList()
          }).catch(e => {
            this.loading = false
          })
        }).catch(e => e)
      } else {
        Message({
          type: 'info',
          message: '请至少选择一条数据！'
        })
      }
    },
    submitEvent () {
      this.$refs.elxEditable.validate(valid => {
        if (valid) {
          let removeRecords = this.pendingRemoveList
          let { insertRecords, updateRecords } = this.$refs.elxEditable.getAllRecords()
          if (insertRecords.length || updateRecords.length || removeRecords.length) {
            insertRecords.forEach(item => {
              if (XEUtils.isDate(item.date)) {
                item.date = item.date.getTime()
              }
            })
            updateRecords.forEach(item => {
              if (XEUtils.isDate(item.date)) {
                item.date = item.date.getTime()
              }
            })
            this.loading = true
            XEAjax.doPost('/api/user/save', { insertRecords, updateRecords, removeRecords }).then(({ data }) => {
              Message({
                type: 'success',
                message: '保存成功!'
              })
              this.findList()
            }).catch(e => {
              this.loading = false
            })
          } else {
            Message({
              type: 'info',
              message: '数据未改动！'
            })
          }
        }
      })
    },
    exportCsvEvent () {
      this.$refs.elxEditable.exportCsv()
    }
  }
}
</script>

<style>
.click-table10-oper {
  margin-bottom: 18px;
}
.click-table10 .delete-row {
  color: #f56c6c;
  text-decoration: line-through;
}
.click-table10-pagination {
  margin-top: 18px;
  text-align: right;
}
.click-table10.elx-editable .elx-editable-row.new-insert,
.click-table10.elx-editable .elx-editable-row.new-insert:hover>td {
  background-color: #f0f9eb;
}
</style>
